<template>
	<div class="home">
		<div class="home_swiper">
			<swiper class="swiper_title" autoplay indicator-dots circular>
				<swiper-item class="swiper_title_item" 
				v-for="(item, index) in swiper" :key={index}>
					<navigator class="title_item_img" >
						<!-- <navigator :url="`/pages/index/detail?id=${item.id}`"> -->
							<img class="item_img" :src="item.img" alt="">
						<!-- </navigator> -->
					</navigator>
				</swiper-item>
			</swiper>
		</div>
		<div class="home_main">
			<div class="main_box" v-for="item in newlist" :key="item.id"
			>
			<div v-if="item.name" >
				<div class="main_box1" >
					<div class="box1_span">
						<span>{{item.desp}}</span>
						<span>{{item.name}}</span>
					</div>
					<img class="box1_img" :src="item.cover" alt="">
				</div>
			</div>
			<div v-if="!item.name" >
				<div class="main_box2">
					<div class="box2_span">
						<span>{{item.desp}}</span>
						<span>{{item.name}}</span>
					</div>
					<img class="box2_img" :src="item.cover" alt="">
				</div>
			</div>
			</div>
			<div></div>
		</div>

	</div>
</template>

<script>
	import { getHomes ,getSwiper} from '@/service/index'
	import './home.css'
	export default {
		data() {
			return {
				swiper:[],
				newlist:[],
				bookOffset: 5,
				videoOffset: 3,
				page:1,
				bodydata:{
					"bookOffset": 0,//5
					"videoOffset": 0//3
				},
				a:0,
				b:0
			}
		}, 
		async onShow(){
			//轮播图数据
			let resswiper = await getSwiper();
			this.swiper=resswiper.ads
			//列表数据
			let reshome = await getHomes(this.bodydata);
			console.log(reshome)
			reshome.books.map(item=>{
				this.newlist.push(item);
			})
			reshome.videos.map(item=>{
				this.newlist.push(item);
			})
			fun(this.newlist)
			function fun(res){
				let len = res.length;
				//从后往前
				for(var i=len-1; i>=0; i--){
					let rand = Math.floor(Math.random()*(i+1));
					let item = res[rand];
					res[rand] = res[i];
					res[i] = item;
				}
				return res;
			}
			console.log(this.newlist)
		},
	 	onReachBottom:  async function () {
			// var that = this;
			// console.log(that)
			// // wx.showLoading({
			// // 	title: '玩命加载中',
			// // })
			// wx.request({
			// 	url: 'https://wechat.wawachina.cn/wxxcx/home/list/3765750',
			// 	data:{
			// 		bookOffset: 5,
			// 		videoOffset: 3,
			// 	},
			// 	method: "POST",
			// 	// 请求头部
			// 	header: {
			// 		'content-type': 'application/json'
      		// 	}
			// })
			this.a = this.a+=5
			this.b = this.b+=3
			this.bodydata = {bookOffset:this.a,videoOffset:this.b}
			console.log(this.bodydata)
			let reshome = await getHomes(this.bodydata);
			let list = []
			reshome.books.map(item=>{
				list.push(item);
			})
			reshome.videos.map(item=>{
				list.push(item);
			})
		 	list = fun(list)
			function fun(res){
				let len = res.length;
				//从后往前
				for(var i=len-1; i>=0; i--){
					let rand = Math.floor(Math.random()*(i+1));
					let item = res[rand];
					res[rand] = res[i];
					res[i] = item;
				}
				return res;
			}
			console.log(list)
			this.newlist = [...this.newlist,...list]
			
		}
	}
</script>